package cn.itrigger.dynamic.sql.where.render;

import cn.itrigger.dynamic.sql.util.FragmentAndParameters;
import cn.itrigger.dynamic.sql.util.FragmentCollector;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author admin
 * @date 2018/9/21
 **/
public class RenderedCriterion {
    private Optional<String> connector;
    private List<RenderedCriterion> subCriteria;
    private FragmentAndParameters initialCondition;

    protected RenderedCriterion(Builder builder) {
        this.connector = Objects.requireNonNull(builder.connector);
        this.subCriteria = Objects.requireNonNull(builder.subCriteria);
        this.initialCondition = builder.initialCondition;
    }

    public FragmentAndParameters renderWithInitialConnector() {
        FragmentAndParameters fp = renderWithoutInitialConnector();
        return connector.map(fp::prependFragment).orElse(fp);
    }

    public FragmentAndParameters renderWithoutInitialConnector() {
        FragmentCollector fc = internalRender();

        String fragment = calculateFragment(fc);
        return FragmentAndParameters.withFragment(fragment)
                .withParameter(fc.parameters())
                .build();
    }

    private String calculateFragment(FragmentCollector collector) {
        if (collector.hasMultipleFragments()) {
            return collector.fragments()
                    .collect(Collectors.joining(" ", "(", ")"));
        } else {
            return collector.fragments().findFirst().orElse("");
        }
    }

    private FragmentCollector internalRender() {
        if (null == initialCondition) {
            return renderSubCriteriaOnly();
        } else {
            return renderConditionAndSubCriteria();
        }
    }

    private FragmentCollector renderConditionAndSubCriteria() {
        return subCriteria.stream()
                .map(RenderedCriterion::renderWithInitialConnector)
                .collect(FragmentCollector.collect(initialCondition));
    }

    private FragmentCollector renderSubCriteriaOnly() {
        FragmentAndParameters initial = subCriteria.get(0).renderWithoutInitialConnector();

        return subCriteria.stream()
                .skip(1)
                .map(RenderedCriterion::renderWithInitialConnector)
                .collect(FragmentCollector.collect(initial));
    }

    public static class Builder {
        private Optional<String> connector;
        private List<RenderedCriterion> subCriteria = new ArrayList<>();
        private FragmentAndParameters initialCondition;

        public Builder withConnector(Optional<String> connector) {
            this.connector = connector;
            return this;
        }

        public Builder withSubCriteria(List<RenderedCriterion> subCriteria) {
            this.subCriteria.addAll(subCriteria);
            return this;
        }

        public Builder withInitialCondition(FragmentAndParameters initialCondition) {
            this.initialCondition = initialCondition;
            return this;
        }

        public Optional<RenderedCriterion> build() {
            if (initialCondition == null && subCriteria.isEmpty()) {
                return Optional.empty();
            }
            return Optional.of(new RenderedCriterion(this));
        }
    }
}
